package com.songcha.library_common.ui.view

import android.app.Activity
import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.marginRight
import androidx.core.view.size
import androidx.fragment.app.Fragment
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.R
import com.songcha.library_common.helper.ViewHelper
import com.songcha.library_common.util.StatusBarUtil
import com.songcha.library_common.util.px2sp
import com.songcha.library_common.util.sp2px


class HeaderBarView : FrameLayout {
    interface IOnHeaderBarGoBackListener{
        fun onGoBack()
    }

    private var mTitle=""
    private var mTvTitle:TextView?=null
    private var mIvBack:ImageView?=null
    private var mllRightBtns:LinearLayout?=null
    private var mOnGoBackListener: IOnHeaderBarGoBackListener? =null
    private var mShowStatusHeight=false
    private var mIsDark=true
    private var mIsTitleStart = false
    private var mIsTitleWrapContent = false
    private var mGravity = 2
    private var mBtns:MutableList<View> = mutableListOf()
    private var mHeight:Int? = null
    private var mIsShowBack = true
    private var mTitleSize = 18f.sp2px()

    companion object{
        val DEFAULT_HEIGHT = 40.dp2px()
        const val GRAVITY_TOP = 1
        const val GRAVITY_CENGER = 2
        const val GRAVITY_BOTTOM = 3
    }
    //private var mShowStatusHeight:Boolean=false

    constructor(context: Context) : super(context) {
        initView(context,null)
    }

    constructor(context: Context,title:String,showStatusHeight:Boolean,height:Int? = null,backgroundColor:Int? = null,isDark:Boolean = true,gravity:Int = 2) : super(context) {
        mTitle = title
        mShowStatusHeight = showStatusHeight
        if(backgroundColor!=null)
            setBackgroundColor(backgroundColor)
        if(height!=null)
            mHeight = height
        mIsDark = isDark
        mGravity = gravity

        initView(context,null)
    }

    constructor(context: Context, attrs: AttributeSet):this(context,attrs,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int):this(context,attrs,defStyle,0){

    }

    constructor(context: Context, attrs: AttributeSet, defStyle:Int, defRes:Int):super(context,attrs,defStyle,defRes){
        initView(context,attrs)
    }


    private fun initView(context: Context, attrs: AttributeSet?){
        if(attrs!=null){
            val ta = context.obtainStyledAttributes(attrs, R.styleable.HeaderBarView)
            mTitle = ta.getString(R.styleable.HeaderBarView_title)?:""
            mShowStatusHeight = ta.getBoolean(R.styleable.HeaderBarView_show_status_height,false)
            mIsDark = ta.getBoolean(R.styleable.HeaderBarView_headerbar_is_dark,true)
            mIsTitleStart = ta.getBoolean(R.styleable.HeaderBarView_headerbar_is_title_start,false)
            mIsTitleWrapContent = ta.getBoolean(R.styleable.HeaderBarView_headerbar_is_title_wrap_content,false)
            mGravity = ta.getInt(R.styleable.HeaderBarView_headerbar_gravity,2)
            mIsShowBack = ta.getBoolean(R.styleable.HeaderBarView_headerbar_is_show_back,true)
            mTitleSize = ta.getDimension(R.styleable.HeaderBarView_headerbar_title_size,18f.sp2px())
            ta.recycle()
        }

        val view = LayoutInflater.from(context).inflate(R.layout.common_view_header_bar,this,false)

        val llParent = view.findViewById<LinearLayout>(R.id.common_ll_parent)

        mTvTitle = view.findViewById(R.id.header_bar_tv_title)
        mTvTitle!!.text = mTitle
        mTvTitle!!.textSize = mTitleSize.px2sp()
        if(mIsTitleStart){
            mTvTitle!!.gravity = Gravity.CENTER_VERTICAL or Gravity.START
            mTvTitle!!.layoutParams = mTvTitle!!.layoutParams.apply {
                this as LayoutParams
                this.leftMargin = DEFAULT_HEIGHT
            }
        }
        if(mIsTitleWrapContent){
            mTvTitle!!.layoutParams = mTvTitle!!.layoutParams.apply {
                width = ViewGroup.LayoutParams.WRAP_CONTENT
            }
        }

        mIvBack = view.findViewById(R.id.iv_back)
        ViewHelper.setVisibility(mIsShowBack,mIvBack)

        mIvBack!!.setOnClickListener {
            if(mOnGoBackListener == null && context is Activity){
                context.finish()
            }else{
                mOnGoBackListener!!.onGoBack()
            }

        }
        if(!mIsDark){
            mTvTitle!!.setTextColor(Color.WHITE)
            mIvBack!!.setImageResource(R.mipmap.common_icon_back_white)
        }

        mllRightBtns = view.findViewById(R.id.header_right_btns_ll)

        when(mGravity){
            1->{
                val lp = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DEFAULT_HEIGHT)
                lp.gravity = Gravity.TOP
                view.layoutParams = lp
                addView(view)
            }
            2->{
                addView(view,ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT)
            }
            3->{
                val lp = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DEFAULT_HEIGHT)
                lp.gravity = Gravity.BOTTOM
                view.layoutParams = lp
                addView(view)
            }
        }

        if(mShowStatusHeight){
            val statusBar = view.findViewById<View>(R.id.hearder_bar_status)
            statusBar.visibility = View.VISIBLE
            statusBar.layoutParams=statusBar.layoutParams.apply {
                if(context is Activity) {
                    height = StatusBarUtil.getStatusBarHeight(context)
                }else if(context is Fragment){
                    height = StatusBarUtil.getStatusBarHeight(context.requireActivity())
                }
            }
        }

        /*if(mGravity == 1){
            mIvBack!!.layoutParams = mIvBack!!.layoutParams.apply {
                this as ConstraintLayout.LayoutParams
                topToTop = ConstraintLayout.LayoutParams.PARENT_ID
                bottomToBottom = ConstraintLayout.LayoutParams.UNSET
            }


            mllRightBtns!!.gravity = Gravity.TOP


        }*/

    }

    fun showBackIcon(isShow:Boolean){
        ViewHelper.setVisibility(isShow,mIvBack)
    }

    fun setStyle(isDark: Boolean){
        if(isDark){
            mTvTitle!!.setTextColor(Color.BLACK)
            mIvBack!!.setImageResource(R.mipmap.common_icon_back)
        }else{
            mTvTitle!!.setTextColor(Color.WHITE)
            mIvBack!!.setImageResource(R.mipmap.common_icon_back_white)
        }
    }

    fun getIvBack():ImageView{
        return mIvBack!!
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        var hms=heightMeasureSpec
        val heightMode=MeasureSpec.getMode(heightMeasureSpec)
        if(heightMode==MeasureSpec.AT_MOST || heightMode==MeasureSpec.UNSPECIFIED){
            var h = if(mHeight == null) DEFAULT_HEIGHT else mHeight!!
            if(mShowStatusHeight){
                if(context is Activity) {
                    h += StatusBarUtil.getStatusBarHeight(context as Activity)
                }else if(context is Fragment){
                    h += StatusBarUtil.getStatusBarHeight((context as Fragment).requireActivity())
                }
                //h+=StatusBarUtil.getStatusBarHeight(context)
            }
            hms = MeasureSpec.makeMeasureSpec(h,MeasureSpec.EXACTLY)
        }
        super.onMeasure(widthMeasureSpec, hms)
    }

    fun setTitle(title:String){
        mTitle = title
        mTvTitle?.text=mTitle
    }

    fun setTitleColor(color:Int){
        mTvTitle?.setTextColor(color)
    }

    fun getTitleView():TextView{
        return mTvTitle!!
    }

    fun setOnGoBackListener(listener: IOnHeaderBarGoBackListener?){
        mOnGoBackListener=listener
    }

    fun addTextButton(text:String,textSize:Float,textColor:Int,marginRight:Int,onClickListener: OnClickListener){
        if(mllRightBtns==null) return
        val tv = TextView(context)
        tv.text = text
        tv.textSize = textSize
        tv.setTextColor(textColor)
        val lp = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT)
        lp.rightMargin = marginRight
        tv.layoutParams = lp
        mllRightBtns!!.visibility = View.VISIBLE
        mllRightBtns!!.addView(tv)
        tv.setOnClickListener(onClickListener)
        mBtns.add(tv)
    }

    fun addImageButton(resId:Int,size:Int,marginRight:Int,onClickListener: OnClickListener){
        if(mllRightBtns==null) return
        val iv = ImageView(context)
        iv.setImageResource(resId)
        iv.scaleType = ImageView.ScaleType.CENTER_CROP
        val lp = LinearLayout.LayoutParams(size,size)
        lp.rightMargin = marginRight
        iv.layoutParams = lp
        mllRightBtns!!.visibility = View.VISIBLE
        mllRightBtns!!.addView(iv)
        iv.setOnClickListener(onClickListener)
        mBtns.add(iv)
    }

    fun getButton(index:Int):View?{
        if(index>mBtns.size-1) return null
        return mBtns[index]
    }
}